package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.ReturnDao;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.entity.ReturnList;
import com.atguigu.jxc.entity.ReturnListGoods;
import com.atguigu.jxc.service.ReturnService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReturnServiceImpl implements ReturnService {
    @Autowired
    private ReturnDao returnDao;

    @Override
    public void saveReturnList(ReturnList returnList, String returnListGoodsStr) {
        //创建Gson 将String转为对象
        Gson gson = new Gson();
        returnDao.saveReturnList(returnList);
        List<Map<String, Object>> list = gson.fromJson(returnListGoodsStr, List.class);
        for (Map<String, Object> map : list) {
            ReturnListGoods returnListGoods = new ReturnListGoods();
            returnListGoods.setGoodsId(Integer.parseInt(map.get("goodsId").toString().split("\\.")[0]));
            returnListGoods.setGoodsCode((String) map.get("goodsCode"));
            returnListGoods.setGoodsName((String) map.get("goodsName"));
            returnListGoods.setGoodsModel((String) map.get("goodsModel"));
            returnListGoods.setGoodsUnit((String) map.get("goodsUnit"));
            returnListGoods.setGoodsNum(Integer.parseInt((String) map.get("goodsNum")));
            Integer price = Integer.parseInt(map.get("price").toString().split("\\.")[0]);
            Integer total = Integer.parseInt(map.get("total").toString().split("\\.")[0]);
            returnListGoods.setReturnListGoodsId(returnList.getReturnListId());
            returnListGoods.setGoodsTypeId(Integer.parseInt(map.get("goodsTypeId").toString().split("\\.")[0]));
            returnDao.saveReturnListGoods(returnListGoods, price, total);
        }
    }

    @Override
    public Map<String, Object> getReturnList(String purchaseNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        List<ReturnList> lists = returnDao.getReturnList(purchaseNumber, supplierId, state, sTime, eTime);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows", lists);
        return map;
    }
    @Override
    public Map<Object, Object> getReturnListGoods(Integer returnListId) {
        List<ReturnListGoods> returnListGoodsList = returnDao.getReturnListGoods(returnListId);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("rows", returnListGoodsList);
        return map;
    }

    @Override
    public void returnDelete(Integer returnListId) {

        //删除退货单对应的商品信息
        returnDao.returnDeleteGoods(returnListId);
        //删除退货单
        returnDao.returnDelete(returnListId);
    }

    @Override
    public List<ReturnList> getReturnCount(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        List<ReturnList> getReturnList = returnDao.getReturnCount(sTime,eTime,goodsTypeId,codeOrName);
        return getReturnList;
    }
}